#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int arr[1010];
void fun()
{
	int k = 1;
	for (int i = 'A'; i <= 'Z'; i++) {
		arr[i] = k++;
	}
	k = -1;
	for (int i = 'a'; i <= 'z'; i++) {
		arr[i] = k--;
	}
}
int main()
{
	int t;
	cin >> t;
	while (t--) {
		char c;
		int n;
		cin >> c >> n;
		fun();
		int sum = 0;
		sum = arr[c] + n;
		cout << sum << endl;
	}
	return 0;
}